﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GameBase;
using GameLayer;
using Microsoft.Xna.Framework.Content;
using System.Xml;
using Microsoft.Xna.Framework;
using System.Reflection;

namespace MainGame.GameForm
{
    public class ButtonControl : Button
    {
        private bool _autoSize = true;
        public bool AutoSize
        {
            get { return _autoSize; }
            set { _autoSize = value; }
        }
        public FormControl Parrent { get; set; }
        public override Vector2 Size
        {
            get
            {
                if (AutoSize)
                    return base.Size;
                return _size;
            }
            set
            {
                _size = value;
            }
        }
        public MethodInfo ClickMethodInfo { get; set; }
        public String NameClickFunction { get; set; }
        public ButtonControl(ContentManager content, String hoverAsset, String normalAsset, String visitedAsset)
        {
            Sprite normal = new Sprite(content, normalAsset, 1);
            Sprite hover = new Sprite(content, hoverAsset, 1);
            Sprite visitted = new Sprite(content, visitedAsset, 1);
            this.Items.AddRange(new[] { normal, hover, visitted });
        }

        public ButtonControl(ContentManager content, Sprite hover, Sprite normal, Sprite visited)
        {
            this.Items.AddRange(new[] { normal, hover, visited });
        }

        public ButtonControl(ContentManager content, XmlElement xml)
        {
            XmlNodeList xmlList = xml.SelectNodes("Sprite");
            XmlElement normal = xml.SelectSingleNode("Sprite[@name='normal']") as XmlElement;
            XmlElement hover = xml.SelectSingleNode("Sprite[@name='hover']") as XmlElement;
            XmlElement visited = xml.SelectSingleNode("Sprite[@name='visited']") as XmlElement;
            Sprite spriteNormal = new Sprite(content, normal);
            Sprite spriteHover = new Sprite(content, hover);
            Sprite spriteVisitted = new Sprite(content, visited);
            this.Items.AddRange(new[] { spriteNormal, spriteHover, spriteVisitted });
            int width = 0, height = 0;
            if (int.TryParse(xml.GetAttribute("width"), out width) |/*dấu hợp bít*/
                int.TryParse(xml.GetAttribute("height"), out height))
            {
                this.Size = new Vector2(width, height);
            }
            if (!bool.TryParse(xml.GetAttribute("autosize"), out _autoSize))
            {
                AutoSize = true;
            }
            int x, y;
            if (!int.TryParse(xml.GetAttribute("x"), out x))
            {
                x = 0;
            }
            if (!int.TryParse(xml.GetAttribute("y"), out y))
            {
                y = 0;
            }
            if (xml.HasAttribute("onClick"))
                this.NameClickFunction = xml.GetAttribute("onClick");
            this.Position = new Vector2(x, y);
        }
        protected override void Paint(GameTime gameTime, Microsoft.Xna.Framework.Graphics.SpriteBatch spriteBatch, object extraData)
        {
            if (AutoSize)
            {
                Current.Draw(gameTime, spriteBatch);
            }
            else
            {
                var pos = this.Position;
                var size = this.Size;
                Rectangle bound = new Rectangle((int)pos.X, (int)pos.Y, (int)size.X, (int)size.Y);
                Current.Draw(gameTime, spriteBatch, bound, Color.White);
            }
        }
        protected override void UpdateEntitys(GameTime gameTime, object extraData)
        {
            base.UpdateEntitys(gameTime, extraData);
            this.Current.Update(gameTime);
            this.Current.NextFrame();
        }
        protected override void OnMouveLeave(Microsoft.Xna.Framework.Input.MouseState mouseState)
        {
            base.OnMouveLeave(mouseState);
            this.Index = 0;
        }

        protected override void OnHover(Microsoft.Xna.Framework.Input.MouseState mouseState)
        {
            base.OnHover(mouseState);
            if (Parrent != null)
            {
                Parrent.ActiveButton = this;
            }
            this.Index = 1;
        }

        protected override void OnClick(Microsoft.Xna.Framework.Input.MouseState mouse)
        {
            base.OnClick(mouse);
            this.Index = 2;
            if (this.NameClickFunction != null)
            {
                if (this.ClickMethodInfo == null)
                {
                    Type parentType = this.Parrent.GetType();
                    this.ClickMethodInfo = parentType.GetMethod(this.NameClickFunction);
                }
                if (ClickMethodInfo == null)
                    throw new Exception("onClick function invalid");
                this.ClickMethodInfo.Invoke(Parrent, null);
            }
        }
    }
}
